/*
* Copyright 2024 - 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.demo.mcp.functions;

import java.time.Duration;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class WeatherFunction_back {

//	private static final String BASE_URL = "https://api.weather.gov";
//
//	private final RestClient restClient;
//
//	public WeatherService() {
//
//		this.restClient = RestClient.builder()
//			.baseUrl(BASE_URL)
//			.defaultHeader("Accept", "application/geo+json")
//			.defaultHeader("User-Agent", "WeatherApiClient/1.0 (your@email.com)")
//			.build();
//	}
//
//	@JsonIgnoreProperties(ignoreUnknown = true)
//	public record Points(@JsonProperty("properties") Props properties) {
//		@JsonIgnoreProperties(ignoreUnknown = true)
//		public record Props(@JsonProperty("forecast") String forecast) {
//		}
//	}
//
//	@JsonIgnoreProperties(ignoreUnknown = true)	public record Forecast(@JsonProperty("properties") Props properties) {
//		@JsonIgnoreProperties(ignoreUnknown = true)
//		public record Props(@JsonProperty("periods") List<Period> periods) {
//		}
//
//		@JsonIgnoreProperties(ignoreUnknown = true)
//		public record Period(@JsonProperty("number") Integer number, @JsonProperty("name") String name,
//				@JsonProperty("startTime") String startTime, @JsonProperty("endTime") String endTime,
//				@JsonProperty("isDaytime") Boolean isDayTime, @JsonProperty("temperature") Integer temperature,
//				@JsonProperty("temperatureUnit") String temperatureUnit,
//				@JsonProperty("temperatureTrend") String temperatureTrend,
//				@JsonProperty("probabilityOfPrecipitation") Map probabilityOfPrecipitation,
//				@JsonProperty("windSpeed") String windSpeed, @JsonProperty("windDirection") String windDirection,
//				@JsonProperty("icon") String icon, @JsonProperty("shortForecast") String shortForecast,
//				@JsonProperty("detailedForecast") String detailedForecast) {
//		}
//	}
//
//	@JsonIgnoreProperties(ignoreUnknown = true)
//	public record Alert(@JsonProperty("features") List<Feature> features) {
//
//		@JsonIgnoreProperties(ignoreUnknown = true)
//		public record Feature(@JsonProperty("properties") Properties properties) {
//		}
//
//		@JsonIgnoreProperties(ignoreUnknown = true)
//		public record Properties(@JsonProperty("event") String event, @JsonProperty("areaDesc") String areaDesc,
//				@JsonProperty("severity") String severity, @JsonProperty("description") String description,
//				@JsonProperty("instruction") String instruction) {
//		}
//	}

	@Tool(name = "streamWeather",
			description = "流式获取城市天气预报，参数为中文城市名")
	public Flux<String> getStreamWeather(String city) {
		String weatherData = switch(city) {
			case "北京" -> "25℃|晴|湿度45%|北风2级";
			case "上海" -> "28℃|多云|湿度65%|东南风3级";
			case "广州" -> "32℃|阵雨|湿度80%|南风4级";
			default -> "暂不支持该城市天气预报";
		};

		return Flux.interval(Duration.ofMillis(500))
				.zipWith(Flux.fromArray(weatherData.split("\\|")))
				.map(tuple -> tuple.getT2());
	}

}